#!/usr/bin/env python
# coding=utf-8
# @FileName  :events.py
# @Time      :2024/6/1 9:58
# @Author    :jdz
from contextlib import asynccontextmanager
from fastapi import FastAPI

from config.get_db import init_create_table
from config.env import AppConfig
from task.crawler_task import start_scheduler
from util.mylog import my_logger


@asynccontextmanager
async def lifespan(app: FastAPI):
    my_logger.info(f'{AppConfig.app_name}开始启动')
    await init_create_table()
    # app.state.redis = await RedisUtil.create_redis_pool()
    # await RedisUtil.init_sys_dict(app.state.redis)
    # await RedisUtil.init_sys_config(app.state.redis)
    # await SchedulerUtil.init_system_scheduler()
    my_logger.info(f'{AppConfig.app_name}启动成功')
    app.state.scheduler = start_scheduler()  # 在应用启动时初始化调度器
    yield
    app.state.scheduler.shutdown()
    # await RedisUtil.close_redis_pool(app)
    # await SchedulerUtil.close_system_scheduler()
